﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using vn.hus.nlp.tokenizer;
namespace EmailClient.FilterSpam
{
    public class Parser
    {
        public Dictionary<string, int> wordCounter = new Dictionary<string, int>();
        //Lấy danh sách email từ chuỗi
        public MatchCollection emas(string text)
        {
            const string MatchEmailPattern =
           @"(([\w-]+\.)+[\w-]+|([a-zA-Z]{1}|[\w-]{2,}))@"
           + @"((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\."
             + @"([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|"
           + @"([a-zA-Z]+[\w-]+\.)+[a-zA-Z]{2,4})";
            Regex rx = new Regex(MatchEmailPattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
            // Find matches.
            MatchCollection matches = rx.Matches(text);
            return matches;
        }

        public List<string> SplitStringCustomize(string strSource)
        {
            string tmpSource = "";
            List<string> listString = new List<string>();
            int i = 0;
            for (; i < strSource.Length; )
            {
                if ((Char.IsLetter(strSource[i]))||(strSource[i].CompareTo('_') == 0)||(Char.IsNumber(strSource[i])))
                {
                    tmpSource += strSource[i].ToString();
                    if (i == strSource.Length - 1)
                    {
                        listString.Add(tmpSource);
                    }
                    i++;
                }
                else
                {
                    //Console.WriteLine(tmpSource);
                    if (tmpSource.CompareTo("") != 0)
                    {
                        listString.Add(tmpSource);
                    }
                    i = i + 1;
                    tmpSource = "";
                }
            }
            return listString;
        }

        public List<string> ExtractEmailAddressFromString(string strSource)
        {
            MatchCollection matchCollection;
            List<string> listString = new List<string>();
            matchCollection = Regex.Matches(strSource, @"([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5})");
            foreach (Match match in matchCollection)
            {
                listString.Add(match.Value);
                strSource = strSource.Replace(match.Value, String.Empty);
            }
            return listString;
        }

        public MatchCollection ExtractLinkWebsiteFromSpring(string strSource)
        {
            List<string> listString = new List<string>();
            Regex linkParser = new Regex(@"\b(?:https?://|www\.)\S+\b", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            MatchCollection matches = linkParser.Matches(strSource);
            return matches;
        }

        public string ReplaceWithEmptyString(MatchCollection matches, string strSource)
        {
            foreach (Match match in matches)
            {
                strSource = strSource.Replace(match.Value, String.Empty);
            }
            return strSource;
        }
        public Dictionary<string, int> Parse(string textToParse)
        {
            MatchCollection matchesEmailAddress = emas(textToParse);
            string strSource = ReplaceWithEmptyString(matchesEmailAddress, textToParse);
            MatchCollection matchesLinkWeb = ExtractLinkWebsiteFromSpring(textToParse);
            strSource = ReplaceWithEmptyString(matchesLinkWeb, strSource);
            //split tu ghep tu don
            VietTokenizer tokenizer = new VietTokenizer();
            string result = tokenizer.segment(strSource);
            var stringArray = SplitStringCustomize(result);
            if (matchesEmailAddress.Count != 0)
            {
                foreach (Match match in matchesEmailAddress)
                {
                    stringArray.Add(match.Value);
                }
            }

            if (matchesLinkWeb.Count != 0)
            {
                foreach (Match match in matchesLinkWeb)
                {
                    stringArray.Add(match.Value);
                }
            }

            foreach (var str in stringArray)
            {
                if(str.CompareTo("")!=0){
                    if (wordCounter.ContainsKey(str.ToLower()))
                    {
                        wordCounter[str.ToLower()] += 1;
                    }
                    else
                    {
                        wordCounter.Add(str.ToLower(), 1);
                    }
                }
            }
            return wordCounter;
        }

        public void UpdateDictionary(Dictionary<string, int> sourceDictionary, Dictionary<string, int> desDictionary)
        {
            foreach (var list in sourceDictionary)
            {
                if (desDictionary.ContainsKey(list.Key))
                {
                    desDictionary[list.Key] += 1;
                }
                else
                {
                    desDictionary.Add(list.Key, 1);
                }
            }

        }
    }
}
